package com.IO;

import java.io.*;
//复制文件夹
public class Copy {
    public static void main(String[] args) throws IOException {
        File src = new File("D:\\下载\\file\\file1");
        File dest = new File("D:\\下载\\file\\file5");
        copyFile(src,dest);
    }

    private static void copyFile(File src, File dest) throws IOException {
        if(src.isDirectory()){
            String srcName=src.getName();
            File newFile=new File(dest,srcName);
            if(!newFile.exists()){
                newFile.mkdirs();
            }
            File[] fileArray = src.listFiles();
            for(File file:fileArray) {
                copyFile(file,newFile);
            }
        }else{
            File newFile=new File(dest,src.getName());
            copy(src,newFile);
        }

    }
//复制具体文件
    private static void copy(File src, File newFile) throws IOException {
        BufferedInputStream bis=new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(newFile,true));
        byte[] bys=new byte[1024];
        int len;
        while((len=bis.read(bys))!=-1){
            bos.write(bys,0,len);
        }
        bis.close();
        bos.close();
    }
}
